package top.jolyoulu.codegenweb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.jolyoulu.codegenweb.entity.bo.form.GenTemplateFileFormBO;
import top.jolyoulu.codegenweb.entity.bo.form.GenTemplateGroupFormBO;
import top.jolyoulu.codegenweb.entity.vo.GenTemplateFileVO;
import top.jolyoulu.codegenweb.entity.vo.GenTemplateGroupVO;
import top.jolyoulu.codegenweb.service.bll.ApiGenTemplateService;
import top.jolyoulu.webcommon.entity.ResultInfo;

import javax.servlet.http.HttpServletResponse;
import java.lang.management.MemoryType;
import java.util.List;

/**
 * 模板管理
 */
@RestController
@RequestMapping("/api/gen/template")
public class ApiGenTemplateController {

    @Autowired
    private ApiGenTemplateService apiGenTemplateService;

    /**
     * 列表查模板询
     */
    @GetMapping("/group/list")
    public ResultInfo<List<GenTemplateGroupVO>> list() {
        List<GenTemplateGroupVO> res = apiGenTemplateService.list();
        return ResultInfo.success(res);
    }

    /**
     * 添加组
     */
    @PostMapping("/group/add")
    public ResultInfo groupAdd(@RequestBody @Validated GenTemplateGroupFormBO bo) {
        apiGenTemplateService.groupAdd(bo);
        return ResultInfo.success();
    }

    /**
     * 编辑模板组
     */
    @PutMapping("/group/edit/{id}")
    public ResultInfo groupEdit(@PathVariable String id, @RequestBody GenTemplateGroupFormBO bo) {
        bo.setId(id);
        apiGenTemplateService.groupEdit(bo);
        return ResultInfo.success();
    }

    /**
     * 删除模板组
     */
    @DeleteMapping("/group/del")
    public ResultInfo groupDel(@RequestBody List<String> ids) {
        apiGenTemplateService.groupDel(ids);
        return ResultInfo.success();
    }

    /**
     * 模板组下模板文件
     */
    @GetMapping("/group/file/list/{groupId}")
    public ResultInfo<List<GenTemplateFileVO>> groupFileList(@PathVariable("groupId") String groupId) {
        List<GenTemplateFileVO> res = apiGenTemplateService.groupFileList(groupId);
        return ResultInfo.success(res);
    }

    /**
     * 下载模板
     */
    @GetMapping("/file/download/{id}")
    public void fileDownload(@PathVariable String id, HttpServletResponse response) {
        apiGenTemplateService.fileDownload(id, response);
    }

    /**
     * 获取模板基础信息
     */
    @GetMapping("/file/{id}")
    public ResultInfo<GenTemplateFileVO> fileById(@PathVariable("id") String id) {
        GenTemplateFileVO res = apiGenTemplateService.fileById(id);
        return ResultInfo.success(res);
    }

    /**
     * 添加模板
     */
    @PostMapping(value = "/file/add", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResultInfo fileAdd(@Validated GenTemplateFileFormBO bo) {
        apiGenTemplateService.fileAdd(bo);
        return ResultInfo.success();
    }

    /**
     * 编辑模板
     */
    @PostMapping(value = "/file/edit/{id}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResultInfo fileEdit(@PathVariable String id, @Validated GenTemplateFileFormBO bo) {
        bo.setId(id);
        apiGenTemplateService.fileEdit(bo);
        return ResultInfo.success();
    }

    /**
     * 删除模板
     */
    @DeleteMapping("/file/del")
    public ResultInfo fileDel(@RequestBody List<String> ids) {
        apiGenTemplateService.fileDel(ids);
        return ResultInfo.success();
    }

}
